import { get, post, upload } from '@/utils/request.js'
import Config from '@/config'
import store from '@/store/index.js'
export default {
  list(data) {
    return post(Config.API_VERSION + '/TargetClients/QueryPages', data)
  },
  listAdmin(data) {
    return post(Config.API_VERSION + '/Admin/TargetClients/QueryPages', data)
  },
  /**
   * 获取新建待审核的用户
   * @param clientName
   * @returns {Promise<unknown>}
   */
  listAdminPendingClient(clientName, status) {
    return get(Config.API_VERSION + '/Admin/TargetClients/GetClientApplyList', { clientName, status })
  },
  /**
   * 删除新建已经被拒绝的记录
   * @param clientID
   */
  deleteAdminRejectedClient(clientID) {
    return get(Config.API_VERSION + '/Admin/TargetClients/DeleteClientOrgApply', { clientID })
  },
  /**
   * 获取长期合作申请用户
   * @param clientName
   * @returns {Promise<unknown>}
   */
  listApplyForExtension(clientName) {
    return get(Config.API_VERSION + '/Admin/Others/ContractExtension/GetClientContractExtensionList', { clientName })
  },
  /**
   * 获取二次开发待审核客户
   * @param clientName
   * @returns {Promise<unknown>}
   */
  listApplyForRevival(clientName) {
    return get(Config.API_VERSION + '/Admin/Reconnect/GetClientReconnectList', { clientName })
  },
  getFilterConditions(clientName, countryID, url, beginTime, endTime) {
    return get(Config.API_VERSION + '/TargetClients/GetTargetClientConditions', {
      clientName,
      countryID,
      url,
      beginTime,
      endTime
    })
  },
  getFilterConditionsAdmin(clientName, countryID, url, bumID, type, selectEmpID, beginTime, endTime, ownerBy) {
    return get(Config.API_VERSION + '/Admin/TargetClients/GetTargetClientConditions', {
      clientName,
      countryID,
      url,
      bumID,
      type,
      selectEmpID,
      beginTime,
      endTime,
      ownerBy
    })
  },
  // 获取BU / Branch
  getAuthority() {
    return get(Config.API_VERSION + '/Admin/TargetClients/GetAuthority', {})
  },
  // 获取BU / Branch 下的人员
  getEmployee(bumId, type) {
    return get(Config.API_VERSION + '/Admin/TargetClients/GetEmployeeSales', { bumId, type })
  },
  // 创建Organization
  createOrganization(name) {
    return post(Config.API_VERSION + '/TargetClients/CreateNewClient/CreateOrganization?organizationName=' + name, {})
  },
  getTimeZoneByCountryID(countryId) {
    return get(Config.API_VERSION + '/TargetClients/CreateNewClient/GetTimeZoneByCountryID', { countryId })
  },
  async isUniqueAbbreviation(rule, value, id) {
    value = value.trim()
    if (value) {
      try {
        const res = await get(Config.API_VERSION + '/TargetClients/CreateNewClient/ShortNameIsExist', {
          shortName: value,
          id
        })
        if (res.data === false) {
          return Promise.resolve()
        } else {
          return Promise.reject(new Error(''))
        }
      } catch (error) {
        return Promise.reject(new Error(''))
      }
    }
  },
  async isUniqueUrl(rule, value, id, parentID) {
    value = value.trim()
    if (value) {
      try {
        const res = await get(Config.API_VERSION + '/TargetClients/CreateNewClient/UrlIsExist', {
          url: value,
          id,
          parentID
        })
        if (res.data === false) {
          return Promise.resolve()
        } else {
          return Promise.reject(new Error(''))
        }
      } catch (error) {
        return Promise.reject(new Error(''))
      }
    }
  },
  isUniqueClientName(fullName, id) {
    fullName = fullName.trim()
    return get(Config.API_VERSION + '/TargetClients/CreateNewClient/FullNameIsExist', { fullName, id })
  },
  isAllowCreate(userId = undefined) {
    return get(Config.API_VERSION + '/TargetClients/CreateNewClient/IsCanCreateClient', {
      userid: userId || store.state.user.userInfo.userID
    })
  },
  // 获取当前人可选BUM，审核用
  getMyBum() {
    return get('/User/GetSalesBUManager', {})
  },
  // 获取审核中的客户
  getPendingClients() {
    return get(Config.API_VERSION + '/TargetClients/GetClientApplyList', {})
  },
  uploadFiles(files) {
    return new Promise((resolve, reject) => {
      upload(files, resolve, reject)
    })
  },
  save(data) {
    return post(Config.API_VERSION + '/TargetClients/CreateNewClient/CreateInfo', data)
  }
}
